package com.um.jdy.mp.baofu.contoller;

import com.um.jdy.common.baofu.entity.po.BaofuUser;
import com.um.jdy.mp.baofu.entity.dto.BaofuUserDTO;
import com.um.jdy.mp.baofu.entity.dto.BaofuUserPageDTO;
import com.um.jdy.mp.baofu.entity.vo.BaofuUserItemVO;
import com.um.jdy.mp.baofu.manager.BaofuManager;
import com.um.jdy.mp.commission.entity.dto.CashoutDTO;
import com.um.springboot.starter.annotation.ApiPermission;
import com.um.springboot.starter.annotation.LoginRequired;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.controller.BaseController;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.params.PageParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 宝付用户管理
 *
 */
@Api(tags = "宝付用户管理")
@Validated
@RestController
@RequestMapping("/api/v1/cms/baofuUser")
@ApiPermission("baofuUser")
@LoginRequired
@Slf4j
public class BaofuUserController extends BaseController<BaofuManager> {
    @ApiPermission("add")
    @ApiOperation("新增")
    @PostMapping("add")
    public Response<BaofuUser> add(@RequestBody @Valid BaofuUserDTO dto) {
        return manager.addEntity(dto);
    }

    /**
     * 分页查询用户
     *
     * @param pageDTO
     * @return
     */
    @ApiPermission("page")
    @ApiOperation("分页查询用户")
    @PostMapping("page")
    public Response<Page<BaofuUserItemVO>> page(@RequestBody BaofuUserPageDTO pageDTO) {
        PageParams pageParams = pageDTO.toPageParams(new String[]{"customerName"},new String[]{"customer_name"});
        pageParams.getCondition().put("orderby", "create_time desc");
        return manager.page(pageParams);
    }

    @ApiPermission("get")
    @ApiOperation("详情")
    @GetMapping({"{id}"})
    public Response<BaofuUser> get(@PathVariable String id) {
        return Response.success(manager.getById(id));
    }

    /**
     * 提现到微信
     * @param cashoutWXDTO
     * @return
     */
    @ApiOperation(value = "提现")
    @PostMapping("cashout")
    public Response cashout(@RequestBody CashoutDTO cashoutWXDTO) {
        manager.BaofuCashout(cashoutWXDTO.getAmount());
        return Response.success();
    }
}
